package com.itdfq.delay.message.listen;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * @Author: QianMo
 * @Date: 2021/10/21 10:45
 * @Description:
 */
@Component
@Slf4j
public class DelayRabbitmqListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
       log.info("收到消息：{}",new String(message.getBody()));
       log.info("message:{}", JSON.toJSONString(message));
       log.info("channel:{}",JSON.toJSONString(channel));

       //2021-10-21 13:59:52.526
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("DelayRabbitmqListener >>>>>>>>消息已消费");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            System.out.println("DelayRabbitmqListener >>>>>>拒绝消息，要求MQ重新发送");
            e.printStackTrace();
            throw e;
        }
    }
    /**
     * 这里可以直接换成@RabbitListener 绑定立即消费的队列即可
     * 然后 发消息 发到延时队列中去
     */
}
